// Copyright (c) 2020 Presto Labs Pte. Ltd.
// Author: jshin

#pragma once

#include <algorithm>
#include <chrono>
#include <cstdint>
#include <string>

#include "coin2/base/log.h"

class ProcessorStat {
 public:
  ProcessorStat() = default;

  int64_t n_packet() const { return n_packet_; }
  int64_t n_exception_packet() const { return n_exception_packet_; }
  int64_t avg_proc_timedelta() const {
    if (n_packet_ > 0) {
      return total_proc_timedelta_ / n_packet_;
    } else {
      return 0;
    }
  }
  int64_t max_proc_timedelta() const { return max_proc_timedelta_; }
  void Add(int64_t begin_ts, int64_t end_ts, bool exception) {
    // arbitration fastfeed converter failed as they are equal
    CHECK_LE(begin_ts, end_ts) << begin_ts << " " << end_ts;
    n_packet_++;
    if (exception) {
      n_exception_packet_++;
    }
    int64_t delta = (end_ts - begin_ts);
    total_proc_timedelta_ += delta;
    max_proc_timedelta_ = std::max(max_proc_timedelta_, delta);
  }
  void PrintDebugMessage(const std::string& topic_name) const {
    VLOG(1) << topic_name << " " << n_packet() << " " << n_exception_packet() << " "
            << avg_proc_timedelta() << " " << max_proc_timedelta();
  }

 protected:
  int64_t n_packet_{0};
  int64_t n_exception_packet_{0};
  int64_t total_proc_timedelta_{0};
  int64_t max_proc_timedelta_{0};
};
